/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/

//server_io.h

#ifndef _SERVER_IO_H
#define _SERVER_IO_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <event.h>
#include <pthread.h>
#include "fnio_define.h"
#include "queue.h"
#include "client_io.h"

struct fnio_client_info;

struct fnio_server_info {
	char ip_addr[IP_ADDRESS_SIZE];  //ip address of the server
	int port;    //port of the true service/server, not the proxy server!
	int sock;    //sock connect to the server
	int last_conn_errno;  //last connect errno to the server

	/* last connect errno proxy server to true service, 
	 * this variable only used by proxy client. this value is notified
	 * by proxy server when the connection status to true service changed
        **/
	int p2s_last_conn_errno; 

 	/* used by proxy server, use this buff to send data to true service */
	struct fnio_buff_node *pSendNode;

	struct fnio_buff_node *pRecvNode; //recv buff from server
	struct fnio_send_buff sbuff;      //send buff to server
	struct event ev_read;
	struct event ev_write;
	bool connected; //if connected to server
	bool sending;  //if in sending progress
	char client_session_id[8];  //proxy client session id, tmp variable
	char server_session_id[8];  //proxy server session id, tmp variable
};

#ifdef __cplusplus
extern "C" {
#endif

int server_io_init();

struct fnio_server_info *fnio_get_server(const int key_hash_code);

int fnio_push_to_server_send_buff(struct fnio_server_info *pServer, \
			struct fnio_client_info *pClientInfo);
int fnio_check_server_connected(struct fnio_server_info *pServer);
int fnio_check_server_connected_by_hash_code(const int key_hash_code);

#ifdef __cplusplus
}
#endif

#endif

